package com.lonely.fx.combo_box;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-18 13:59
 * @author: ztkj-hzb
 * @description:
 **/
public class 修改数据源效果演示 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();

        ComboBox<String> comboBox = new ComboBox<>();
        comboBox.getItems().addAll("篮球","足球","羽毛球");

        comboBox.setEditable(true);
        comboBox.getSelectionModel().selectFirst();

        Button updateBtn = new Button("修改");



        updateBtn.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                //获取索引
                int selectedIndex = comboBox.getSelectionModel().getSelectedIndex();
                comboBox.getItems().set(selectedIndex,comboBox.getValue());
            }
        });


        hBox.getChildren().addAll(comboBox,updateBtn);
        hBox.setSpacing(50);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();
    }
}
